package com.xuqm.sdhbwfu.app.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xuqm.base.di.manager.HttpManager
import com.xuqm.base.extensions.showMessage
import com.xuqm.sdhbwfu.app.data.RiskTodoConfirmData
import com.xuqm.sdhbwfu.app.model.*
import com.xuqm.sdhbwfu.app.repository.Service
import com.xuqm.sdhbwfu.core.extensions.io_main
import com.xuqm.sdhbwfu.core.extensions.subscribeBy
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class RiskTodoViewModel : BaseViewModel() {

    private val _recordList = MutableLiveData<List<RecordModel>>()
    val recordList: LiveData<List<RecordModel>> = _recordList
    fun getRecordList(workNum: String) {
        HttpManager.getApi(Service::class.java)
            .recordList(workNum)
            .io_main()
            .subscribeBy(
                {
                    _recordList.postValue(it)
                }, {
                    showMessage(it)
                    _recordList.postValue(null)
                }
            ).adds()
    }

    private val _applyForm = MutableLiveData<ApplyformModel>()
    val applyForm: LiveData<ApplyformModel> = _applyForm
    fun applyform(workNum: String, taskId: String) {
        HttpManager.getApi(Service::class.java)
            .applyform(workNum, taskId)
            .io_main()
            .subscribeBy(
                {
                    _applyForm.postValue(it)
                }, {
                    showMessage(it)
                    _applyForm.postValue(null)
                }
            ).adds()
    }

    private val _judgmentForm = MutableLiveData<JudgmentformModel>()
    val judgmentForm: LiveData<JudgmentformModel> = _judgmentForm
    fun judgmentform(workNum: String, taskId: String) {
        HttpManager.getApi(Service::class.java)
            .judgmentform(workNum, taskId)
            .io_main()
            .subscribeBy(
                {
                    _judgmentForm.postValue(it)
                }, {
                    showMessage(it)
                    _judgmentForm.postValue(null)
                }
            ).adds()
    }

    private val _managementForm = MutableLiveData<ManagementformModel>()
    val managementForm: LiveData<ManagementformModel> = _managementForm
    fun managementform(workNum: String, taskId: String) {
        HttpManager.getApi(Service::class.java)
            .managementform(workNum, taskId)
            .io_main()
            .subscribeBy(
                {
                    _managementForm.postValue(it)
                }, {
                    showMessage(it)
                    _managementForm.postValue(null)
                }
            ).adds()
    }

    private val _confirmForm = MutableLiveData<ManagementformModel>()
    val confirmForm: LiveData<ManagementformModel> = _confirmForm
    fun confirmform(data: RiskTodoConfirmData) {
        HttpManager.getApi(Service::class.java)
            .confirmform(data)
            .io_main()
            .subscribeBy(
                {
                    _confirmForm.postValue(it)
                }, {
                    showMessage(it)
                    _confirmForm.postValue(null)
                }
            ).adds()
    }


    private val _status = MutableLiveData<RiskUploadModel>()
    val status: LiveData<RiskUploadModel> = _status
    fun annexManagementform(logFile: File) {
        val fileBody: RequestBody =
            RequestBody.create(MediaType.parse("application/octet-stream"), logFile)
        val body = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", logFile.name, fileBody)
            .build()

        HttpManager.getApi(Service::class.java)
            .annexManagementform(body)
            .io_main()
            .subscribeBy(
                {
                    _status.postValue(it)
                }, {
                    _status.postValue(null)
                    showMessage("上传图片失败::$it")
                }
            ).adds()
    }


}